<template>
	<view :class="{bg141414:theme}" style="min-height: 100vh;">

		<view class="con size25 pt50">
			<back :title="type=='level2password'?'交易密码设置':'登录密码设置'"></back>
			<view :class="theme?'bg292929 colorfff':'bgF5F6F7'" class=" ra20 pg30-30">
				<input :class="theme?'bg141414 colorfff':'bgfff'" class="borderE1E1E1 pg30 ra20 size25" v-model="form.phone" placeholder="请输入手机号码" />

				<view class=" mt30 borderE1E1E1 ra20">
					<view @click.stop="tapsend" v-if="!butdisabled"
						class="copde mt30 flex-ju-c size22 posiab zindex50 right90 ">获取验证码</view>

					<view v-else class="copde mt30 flex-ju-c size22 posiab zindex50 right90 color999">已发送 {{ codetxt }}
					</view>
					<input :class="theme?'bg141414 colorfff':'bgfff'" v-model="form.code" class="  ra20 pg30 size25 " placeholder="请输入验证码" />
				</view>
				<input type="password" :class="theme?'bg141414 colorfff':'bgfff'" class="borderE1E1E1 pg30 ra20 mt30 size25" v-model="form.pass" placeholder="请输入新密码" />
				<input type="password" :class="theme?'bg141414 colorfff':'bgfff'" class="borderE1E1E1 pg30 ra20 mt30 size25" v-model="form.password_2"
					placeholder="请确认新密码" />

			</view>

		</view>

		
		<view class="posifi  bottom20 zindex10 width1000 hidden ra20"
			>
				<view @click="tapsrues" class="srgb bg000 con height100 size29  colorfff flex-ju-c ra20 bold" :class="theme?'color000':''">
					确认
				</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				theme: uni.getStorageSync('theme'),
				type: '',
				codetxt: 60,
				timer: "",
				butdisabled: false,
				form: {
					phone: uni.getStorageSync('phone'),
					pass: '',
					setpass: '',
					code: '',
					password_2: ''
				}
			}
		},
		onLoad(option) {
			this.type = option.type
		},
		methods: {
			tapsrues() {
				if (!this.form.code) {
					uni.showToast({
						title: "请输入验证码",
						icon: "none"
					})
					return
				}

				if (this.type === 'password') {
					if (!this.form.pass || !this.form.password_2) {
						uni.showToast({
							title: "请输入密码",
							icon: "none"
						})
						return
					}
					if (this.form.pass != this.form.password_2) {
						uni.showToast({
							title: "密码不一致",
							icon: "none"
						})
						return
					}

					// 修改登录密码
					this.http('user/passwordbyphone', {
						phone:this.form.phone,
						password: this.form.pass,
						password_2: this.form.password_2,
						phone_code: this.form.code
					}, 'post').then(res => {
						uni.showToast({
							title: "重置成功",
							icon: "none"
						})
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/login"
							})
						}, 800)
					})
				}
				if (this.type == 'level2password') {
					// 修改交易密码
					if (this.form.pass.length != 6) {
						uni.showToast({
							title: "交易密码必须6位",
							icon: "none"
						})
						return
					}
					if (!this.form.pass || !this.form.password_2) {
						uni.showToast({
							title: "请输入密码",
							icon: "none"
						})
						return
					}
					if (this.form.pass != this.form.password_2) {
						uni.showToast({
							title: "密码不一致",
							icon: "none"
						})
						return
					}
					this.http('user/level2password', {
						level2password: this.form.pass,
						level2password_2: this.form.password_2,
						phone_code: this.form.code
					}, 'post').then(res => {
						uni.showToast({
							title: "修改成功",
							icon: "none"
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					})
				}
			},
			tapsend(captchaResult) {
				if (!this.form.phone) {
					uni.showToast({
						title: "请输入手机号",
						icon: "none"
					})
					return
				}
				if (this.form.phone.trim().length != 11) {
					uni.showToast({
						title: "手机号错误",
						icon: "none"
					})
					return
				}

				this.http('phone-code', {
					phone: this.form.phone,
					value: this.type,
				}, 'post').then(res => {
					uni.showToast({
						title: "发送成功",
						icon: "none"
					})
					this.butdisabled = true
					this.codetxt = 60
					this.timer = setInterval(() => {
						this.codetxt--
						if (this.codetxt < 1) {
							this.codetxt = 1
							clearInterval(this.timer)
							this.butdisabled = false
						}
					}, 1000)
				}) 
			}
		}
	}
</script>

<style>

</style>